<%@ page language="java" pageEncoding="UTF-8"%>
<script type="text/javascript">

var roleId = $.windowLocationSearch()["id"];

var viewModal = {};

var detailValidator;

var editAction = "${param.action}";

function saveDetail() {
	if (!detailValidator.form()) {
		return;
	}
	$.ajax({
		type: "POST",
		url: "save.json",
		data: viewModal,//data,
		dataType:"json",
		success: function(data) {
			if (!data.succeed) {
				$.notice(data.message);
				return;
			}

			$("#roleId").val(data.data.id);
			
			if (editAction = "new") {
				window.location.href = "edit.do?id="+data.data.id;
			}
			else {
				$.info("已保存");
			}
		},
		error:function(message){
			$.alert(message);
		}
	});
}

function initDetail() {
	if(roleId == undefined || roleId == ""){
		$.alert("无角色ID");
		return;
	}

	$.ajax({
		type: "GET",
		url: "detail.json",
		data: {id:roleId},
		success: function(result) {
			if (!result.succeed) {
				$.alert(result.message);
				return;
			}

			viewModal = result.data;
			delete viewModal.users;
			ko.applyBindings(viewModal);
		},
		dataType:"json",
		error:function(message){
			$.alert(message);
		}
	});

	detailValidator = $("#detailForm").validate({
		rules: {
			roleName:{required:true},
			roleCode:{required:true}
		},
		messages:{
			roleName:{required:"请输入名称"},
			roleCode:{required:"请输入编码"}
		}
	});
	
};

</script>


<form id="detailForm" class="form-horizontal">
<input data-bind="value:id" type="hidden" id="roleId" value="${data.id}">
	<fieldset>
		<div class="control-group">
			<label for="roleName"  class="control-label">名称：</label>
			<div class="controls">
				<input data-bind="value:name" type="text" id="roleName" name="roleName" value="${data.name}"> <span class="cms_color_required">*</span>
			</div>
		</div>
		<div class="control-group">
			<label for="roleCode"  class="control-label">编码：</label>
			<div class="controls">
				<input data-bind="value:code" type="text" id="roleCode" name="roleCode" value="${data.code}"> <span class="cms_color_required">*</span>
			</div>
		</div>
		<div class="control-group">
			<label for="roleDesc"  class="control-label">描述：</label>
			<div class="controls">
				<textarea data-bind="value:description" id="roleDesc" name="roleDesc">${data.description}</textarea>
			</div>
		</div>
		<div class="form-actions">
			<a href="#" class="btn btn-primary" onclick="saveDetail()"><i class="icon-ok"></i>保存</a>
		</div>
	</fieldset>
</form> 
